package com.once;

import android.app.Application;
import android.database.sqlite.SQLiteDatabase;

import com.hjq.toast.ToastUtils;
import com.once.db.DaoMaster;
import com.once.db.DaoSession;
import com.once.db.POJODao;
import com.xuexiang.xui.XUI;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * @author 张成
 */
public class MyApplication extends Application {
    private DaoSession daoSession;

    @Override
    public void onCreate() {
        super.onCreate();
        ToastUtils.init(this);
        XUI.init(this);
        XUI.debug(true);
        initGreenDao();
    }

    /**
     * 初始化GreenDao,直接在Application中进行初始化操作
     */
    private void initGreenDao() {

        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "once.db");
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
        new Thread(() -> daoSession.queryBuilder(POJO.class)
                .where(POJODao.Properties.Time.lt(new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(new Date())))
                .buildDelete()
                .executeDeleteWithoutDetachingEntities()).start();

    }

    public DaoSession getDaoSession() {
        return daoSession;
    }
}
